﻿Imports System.Data.OleDb
Imports System.Data.OleDb.OleDbException
Imports System.Data.DataSet
Imports System.Configuration
Public Class Insumo
    Public connDB As OleDb.OleDbConnection
    Dim cmd As OleDb.OleDbCommand
    Public DataSet1 As DataSet
    Private Sub Insumos_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        listar()
        listar_combo()
    End Sub

    Public Sub conexion()
        Try
            Dim conString As String = ConfigurationManager.ConnectionStrings("conDB").ConnectionString
            connDB = New OleDb.OleDbConnection(conString)
            'MsgBox("conextado corextamente")
        Catch ea As Exception
            MsgBox(ea.Message)
            connDB.Close()
        End Try
    End Sub

    Public Sub listar()
        conexion()
        Dim MiDataSet As New DataSet
        Dim MiAdaptador As New OleDb.OleDbDataAdapter
        Try
            connDB.Open()
            Dim sql As String = "SELECT ID_insumo, Nombre_insumo, Unidad_medida, Peso, Costo, Stock, Flg_inv FROM Insumos "

            Dim comando As New OleDbCommand(sql, connDB)
            MiAdaptador.SelectCommand = comando
            MiAdaptador.Fill(MiDataSet)
            DataGridView1.DataSource = MiDataSet.Tables(0)

        Catch ex As Exception
            MessageBox.Show("error " & Err.Number.ToString & vbCrLf & ex.Message)
            connDB.Close()
        End Try
    End Sub

    Public Sub listar_combo()
        ComboBox1.Items.Add("Balde")
        ComboBox1.Items.Add("Bolsa")
        ComboBox1.Items.Add("Caja")
        ComboBox1.Items.Add("empaque")
        ComboBox1.Items.Add("Frasco")
        ComboBox1.Items.Add("Galon")
        ComboBox1.Items.Add("kilo")
        ComboBox1.Items.Add("Litro")
        ComboBox1.Items.Add("saco")
        ComboBox1.Items.Add("unidad")


        ComboBox2.Items.Add("SI")
        ComboBox2.Items.Add("NO")
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ls_Unidad_medida As String = ComboBox1.SelectedItem.ToString
        Dim ls_Flg_inv As String = ComboBox2.SelectedItem.ToString
        Dim li_Flg_inv As Integer
        If ls_Flg_inv = "SI" Then
            li_Flg_inv = 1
        Else
            li_Flg_inv = 0
        End If
        Try
            conexion()
            connDB.Open()
            cmd = New OleDb.OleDbCommand("INSERT INTO Insumos( Nombre_insumo, Unidad_medida, Peso, Costo, Stock, Flg_inv ) values ('" & TextBox1.Text & "', '" & ls_Unidad_medida & "', " & TextBox2.Text & ", " & TextBox4.Text & ", " & TextBox5.Text & ", " & li_Flg_inv & ")", connDB)
            cmd.ExecuteNonQuery()
            connDB.Close()
            MsgBox("Insumo Registrado Exitosamente")
            listar()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim ls_Unidad_medida As String = ComboBox1.SelectedItem.ToString
        Dim ls_Flg_inv As String = ComboBox2.SelectedItem.ToString
        Dim li_Flg_inv As Integer
        If ls_Flg_inv = "SI" Then
            li_Flg_inv = 1
        Else
            li_Flg_inv = 0
        End If
        Try
            conexion()
            connDB.Open()
            cmd = New OleDb.OleDbCommand("UPDATE Insumos SET Nombre_insumo = '" & TextBox1.Text & "', Unidad_medida = '" & ls_Unidad_medida & "', Peso = " & TextBox2.Text & ", Costo = " & TextBox4.Text & ", Stock = " & TextBox5.Text & ", Flg_inv = " & li_Flg_inv & " WHERE ID_insumo = " & TextBox7.Text, connDB)
            cmd.ExecuteNonQuery()
            connDB.Close()
            MsgBox("La Actualización del Insumo fue Exitosa")
            listar()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Try
            conexion()
            connDB.Open()
            cmd = New OleDb.OleDbCommand("DELETE FROM Insumos WHERE ID_insumo = " & TextBox7.Text, connDB)
            cmd.ExecuteNonQuery()
            connDB.Close()
            MsgBox("El Insumo fue Eliminado Exitosamente")
            listar()
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
        Dim ls_flg_inf1 As String = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(6).Value
        Dim ls_flg_inf As String

        If ls_flg_inf1 = "1" Then
            ls_flg_inf = "SI"
        Else
            ls_flg_inf = "NO"
        End If

        TextBox1.Text = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(1).Value
        TextBox2.Text = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(3).Value
        TextBox4.Text = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(4).Value
        TextBox5.Text = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(5).Value
        TextBox7.Text = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(0).Value

        ComboBox1.SelectedItem = Me.DataGridView1.Rows(DataGridView1.CurrentRow.Index).Cells(2).Value
        ComboBox2.SelectedItem = ls_flg_inf

    End Sub
End Class